﻿using System;

public class ComplexNumber
{
    private double realPart;
    private double imaginaryPart;

    public ComplexNumber(double realPart, double imaginaryPart)
    {
        this.realPart = realPart;
        this.imaginaryPart = imaginaryPart;
    }

    public static ComplexNumber operator +(ComplexNumber firstNumber, ComplexNumber secondNumber)
    {
        double sumRealPart = firstNumber.realPart + secondNumber.realPart;
        double sumImaginaryPart = firstNumber.imaginaryPart + secondNumber.imaginaryPart;
        return new ComplexNumber(sumRealPart, sumImaginaryPart);
    }

    public static ComplexNumber operator -(ComplexNumber firstNumber, ComplexNumber secondNumber)
    {
        double substractionRealPart = firstNumber.realPart - secondNumber.realPart;
        double substractionImaginaryPart = firstNumber.imaginaryPart - secondNumber.imaginaryPart;
        return new ComplexNumber(substractionRealPart, substractionImaginaryPart);
    }

    public static ComplexNumber operator *(ComplexNumber firstNumber, ComplexNumber secondNumber)
    {
        double multiplicationRealPart = firstNumber.realPart * secondNumber.realPart -
            firstNumber.imaginaryPart * secondNumber.imaginaryPart;
        double multiplicationImaginaryPart = firstNumber.realPart * secondNumber.imaginaryPart +
            secondNumber.realPart * firstNumber.imaginaryPart;
        return new ComplexNumber(multiplicationRealPart, multiplicationImaginaryPart);
    }

    public static ComplexNumber operator /(ComplexNumber firstNumber, ComplexNumber secondNumber)
    {
        double realPartNumerator = firstNumber.realPart * secondNumber.realPart +
            firstNumber.imaginaryPart * secondNumber.imaginaryPart;
        double imaginaryPartNumberator = firstNumber.imaginaryPart * secondNumber.realPart -
            firstNumber.realPart * secondNumber.imaginaryPart;
        double denominator = secondNumber.realPart * secondNumber.realPart +
            secondNumber.imaginaryPart * secondNumber.imaginaryPart;
        double devidedRealPart = realPartNumerator / denominator;
        double devidedImaginaryPart = imaginaryPartNumberator / denominator;
        return new ComplexNumber(devidedRealPart, devidedImaginaryPart);
    }

    public static implicit operator ComplexNumber(double value)
    {
        return new ComplexNumber(value, 0);
    }

    public static explicit operator double(ComplexNumber complexNumber)
    {
        double doubleValue = complexNumber.realPart;
        return doubleValue;
    }

    public double this[int index]
    {
        get
        {
            if (index == 0)
            {
                return this.realPart;
            }
            else if (index == 1)
            {
                return this.imaginaryPart;
            }
            else
            {
                throw new IndexOutOfRangeException(String.Format("Index {0} is not valid", index));
            }
        }
    }

    public override string ToString()
    {
        bool imaginaryPartIsNegative = this.imaginaryPart <= 0;
        string imaginaryPartOperator = "+";
        if (imaginaryPartIsNegative)
        {
            imaginaryPartOperator = "-";
            this.imaginaryPart = -this.imaginaryPart;
        }
        string toString = string.Format("{0} {1} {2}i", realPart, imaginaryPartOperator, imaginaryPart);
        return toString;
    }
}

